package doublePointer;

import java.util.ArrayList;

public class FindNumberWithSum {

    /**
     * JZ42 和为S的两个数字
     * @param args
     */
    public static void main(String[] args) {

    }

    public ArrayList<Integer> FindNumbersWithSum(int [] array, int sum) {
        ArrayList<Integer> res = new ArrayList<>();
        int multi = Integer.MAX_VALUE;
        int low = 0;
        int high = array.length - 1;
        int left = 0;
        int right = 0;
        while (low < high){
            if (array[low] + array[high] > sum){
                high--;
            } else if (array[low] + array[high] < sum){
                low++;
            } else {
                int tmp = array[low] * array[high];
                if (tmp < multi){
                    multi = tmp;
                    left = low;
                    right = high;
                }
                high--;
            }
        }
        if (left != right){
            res.add(array[left]);
            res.add(array[right]);
        }
        return res;
    }
}
